package com.navidove.commons.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateFormatUtils;

public class DateUtils extends org.apache.commons.lang.time.DateUtils {

	public static final String DEFAULT_DATE_PATTERN = "yyyy/MM/dd";
	
	public static String format(Date date) {
	    return format(date, DEFAULT_DATE_PATTERN);
	}
	
	public static String format(Date date, String pattern) {
	    if (date == null) return null;
	    return DateFormatUtils.format(date, pattern);
	}
	

	/**
	 * @param year
	 * @param month
	 * @param day
	 * @return
	 */
	public static Date newDate(int year, int month, int day) {
		Calendar cal = truncate(Calendar.getInstance(), Calendar.DATE);
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month);
		cal.set(Calendar.DATE, day);

		return cal.getTime();
	}

	public static Date getGMTDateTime(Date date) {
	    return new Date(date.getTime()-TimeZone.getDefault().getOffset(date.getTime()));
	}

	public static Date formateGMTtoDateTime(Date gmtDate) {
		return formateGMTtoDateTime(TimeZone.getDefault(), gmtDate);
	}
	
	public static Date formateGMTtoDateTime(TimeZone timeZone, Date gmtDate) {
	    return new Date(gmtDate.getTime()+timeZone.getOffset(gmtDate.getTime()));
	}
}
